<?php
	/**
 	 * 消息模块工具类
 	 *
 	 * 消息模块工具类的注意及说明
 	 * 以下use模块请不要互相伤害
  	 *
 	 * @author    xiecz <xiecz@yunys.com.cn>
 	 * @copyright 2016 QuanZhouYunYiShang Inc.
 	 */
	namespace App\Services\Tool\Message;

	use DB,Log;
	use App\Services\Tool\DataBase\SQL;
	use App\Services\Tool\GeneralTool;


	class MessageTool{



		//获取已删除消息编号
		function getDelMessageId($input){
			$id	 = SQL::lists(['table'=>'push_reads',
								'where'=>$input,
								'lists'=>'push_id']);
			return $id;
		}
		//获取已读消息数量
		function getReadMessageCount($input){
			$count = SQL::count(['table'=>'push_reads',
								 'where'=>$input]);
			return $count;
		}

		/**
			获取用户所有消息数
			参数：
				user_id 		用户编号
		 */
		function getUserAllMessageCount($input){
			$count = DB::table('push_messages')
			         ->where($input['where'])
					 ->whereIn('user_id',array($input['user_id'],'0'))
					 ->where('created_at','>',$input['created_at'])
					 ->count();
			return $count;
		}

		//获取对应分类未读消息数
		function getUnReadMessageCount($input){
			$read   = $this->getReadMessageCount(['user_id'=>$input['user_id'],'class'=>$input['class']]);
			$all 	= $this->getUserAllMessageCount(['user_id'=>$input['user_id'],'where'=>['class'=>$input['class']],'created_at'=>$input['created_at']]);
			return ($all-$read)<0?0:($all-$read);
		}

		//获取用户所有消息未读
		function getAllMessageCount($input){

			$lists = SQL::lists(['table'=>'push_class',
								 'where'=>['state'=>1],
								 'lists'=>'class']);

			$count = 0;
			foreach($lists as $li){
				$count += $this->getUnReadMessageCount(['user_id'=>$input['user_id'],'class'=>$li,'created_at'=>$input['created_at']]);
			}

			return $count;
		}


		//获取所有消息列表
		function getUserAllMessage($input){
			$result = DB::table('push_messages')
					  ->where(['class'=>$input['class']])
					  ->whereIn('user_id',[$input['user_id'],0])
					  ->whereNotIn('id',$input['id'])
					  ->where('created_at','>',$input['created_at'])
					  ->orderBy('created_at','desc')
					  ->paginate($input['per_page'])
					  ->toArray();

			$data = [];
			//dd($result);
			foreach($result['data'] as $re){
				//获取详情
				$data[]	= $this->getClassPushInfo($re);

				//标记为已读
				$this->updatePushRead($re,$input);
			}

			$result['data'] = $data;

			return $result;
		}

		/**
			获取对应消息详情
		 */
		function getClassPushInfo($obj){
			$result = (object)array();

			if(isset($obj->class)){
				$result->class = $obj->class;
				$result->push_no = $obj->push_no;
				$result->push_id	= $obj->id.'';
				$result->title = isset($obj->title)?$obj->title:'';
				$result->message = isset($obj->message)?$obj->message:'';
				//dd(json_decode($result->message,true));
				$result->type = isset($obj->type)?$obj->type:2;
				$result->jump_value = isset($obj->jump_value)?$obj->jump_value:'';
				$result->created_at = isset($obj->created_at)?$obj->created_at:time();
				$result->created_at_str = GeneralTool::formatTime($result->created_at);
				$result->is_read = 1;
				$result->template_no = isset($obj->template_no)?$obj->template_no:'';

				//template_no: 1主题活动2认证通知3订单4进账通知5提现提醒
				switch($result->template_no){
					case 1:
					case 3:
					case 4:
						$result->images = $this->getMessageImages(['push_no'=>$result->push_no,'state'=>1]);
						break;
					default:
						$result->images = [];
						break;
				}

			}
			return $result;
		}

		/**
		  	获取消息图片
		 */
		function getMessageImages($input){
			$images = SQL::lists(['table'=>'push_message_images',
								  'where'=>$input,
								  'lists'=>'thumbnail']);
			$arr = [];
			foreach($images as $img){
				$arr[] = env('IMG_URL_STD_PREFIX') . $img;
			}
			return $arr;
		}

		/**
			获取消息类型
		 */
		function getMessageClass($input){
			$class = SQL::pluck(['table'=>'push_messages',
								 'where'=>$input,
								 'pluck'=>'class']);
			return $class;
		}

		/**
			删除消息记录
		 */
		function delMessage($input){
			//判断推送消息是否存在
			if(SQL::count(['table'=>'push_messages',
						   'where'=>['id'=>$input['push_id']]])!=1){
				return GeneralTool::createResult('','M03030');
			}

			$result = 0;

			//判断是否有记录
			if(SQL::count(['table'=>'push_message',
						   'where'=>['user_id'=>$input['user_id'],
									 'push_id'=>$input['push_id'],
									 'class'=>$input['class']]])<1){
				//没有记录
				//插入
				$result = SQL::insert(['table'=>'push_reads',
									   'insert'=>['push_id'=>$input['push_id'],
									   			  'user_id'=>$input['user_id'],
									   			  'class'=>$input['class'],
									   			  'is_del'=>1]]);
			}else{
				//有
				//更新
				$result = SQL::update(['table'=>'push_reads',
									   'where'=>['user_id'=>$input['user_id'],
									   			 'push_id'=>$input['push_id'],
									   			 'class'=>$input['class']],
									   'update'=>['is_del'=>1]]);
			}
			return $result;
		}

		/**
		   添加消息已读记录
		   参数：
		 		user_id			用户编号
		 		id				推送编号
				class			类别
		 */
		function updatePushRead($re,$input){
			//判断参数是否足够
			if(!isset($input['user_id'])||!isset($re->id)||!isset($re->class)){
				return false;
			}

			//判断是否已经存在已读记录
			if(DB::table('push_reads')->where(array('push_id'=>$re->id,'user_id'=>$input['user_id'],'class'=>$re->class))->count()==0){
				$result	= DB::table('push_reads')
						  ->insert(array('push_id'=>$re->id,
										 'user_id'=>$input['user_id'],
										 'created_at'=>time(),
										 'class'=>$re->class));
			}

			return true;
		}

		/**
			消息类别列表
		 */
		function getPushClassList($input){
			$class = DB::table('push_class')
					  ->where($input['where'])
					  ->orderBy('sort')
					  ->select('class','class_name','icon')
					  ->get();
			foreach($class as $cla){
				$cla->unread_num = $this->getUnReadMessageCount(['user_id'=>$input['user_id'],'created_at'=>$input['created_at'],'class'=>$cla->class]);
				$cla->icon = env('IMG_URL_STD_PREFIX') . $cla->icon;
			}
			return $class;
		}


	}
